package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import controller.AnalyserCorpus;
import controller.PlusRare;
import metier.ChargerCorpus;
import metier.ChargerFichier;
import metier.ConfigFiltre;
import metier.DecouperFichier;
import metier.EcrireFichier;
import metier.Filtre;

public class FRare extends JFrame {

	JLabel path;
	JComboBox corpus = new JComboBox<>();
	JButton parcourir, valider, executer, ajouter, ok, sauvegarder,supprimer;
	JTextField newMot,pathFile;
	JTable tabFrequent;
	DefaultTableModel model ;
	Object[][] datas;
	ConfigFiltre config;
	String pathCorpus, nomCorpus;
	List<String> lCorpus = new ArrayList<>();
	List<String> liste = new ArrayList<>();
	int index = 0;

	public FRare() {
		super();
		init();
		setLayout(new BorderLayout());
		add(topPanel(), BorderLayout.CENTER);
		
		JPanel bot2 = new JPanel();
		bot2.setLayout(new GridLayout(1,2));
		sauvegarder = new JButton("Sauvegarder");
		sauvegarder.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String str = pathFile.getText();
				List<String> res = new ArrayList<String>();
				for(int i = 0; i < model.getRowCount(); i++){
					res.add(model.getValueAt(i, 0).toString());
				}
				EcrireFichier.ecrireFichier("filtres/moinsFrequent/"+str+".txt", res);
			}
		});
		pathFile = new JTextField();
		bot2.add(pathFile);
		bot2.add(sauvegarder);
		add(bot2,BorderLayout.SOUTH);

		File folder = new File("fichiersOriginaux");
		for(File f : folder.listFiles()) {
			if(!f.getName().equals(".DS_Store")){
				corpus.addItem(f.getName());
				lCorpus.add(f.getAbsolutePath());
			}
		}

		setVisible(true);
	}

	public void init() {
		setTitle("Fouille De Texte - 2015");
		setSize(new Dimension(470, 400));
		setLocationRelativeTo(null);

		config = new ConfigFiltre();
	}

	public JPanel topPanel() {
		JPanel res = new JPanel();
		res.setLayout(new BorderLayout());

		// Partie parcourir
		JPanel top = new JPanel();
		top.setLayout(new GridLayout(1, 3));
		top.add(corpus);

		executer = new JButton("Trouver les rares");
		top.add(executer);
		executer.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Model : "+model.getRowCount());
				for (int i = model.getRowCount()-1; i >= 0; i--) {
					model.removeRow(i);
				}
				
				String separatorPath = File.separator;
				String[] tab = lCorpus.get(corpus.getSelectedIndex()).split(Pattern.quote(separatorPath));
				nomCorpus = tab[tab.length-1];

				liste = new ArrayList<>();
				liste = PlusRare.plusRareController(nomCorpus);
				
				Object[] data = new Object[1];
				datas = new Object[liste.size()][2];
				data[0] = "Mot";
				int max = 0;
				model.setColumnIdentifiers(data);
				index = 0;
				for (String mot : liste) {
					
					datas[index][0] = liste.get(index);
					datas[index][1] = "Supprimer";
					
					model.addRow(datas[index]);
					++index;
				}

			}
		});
		
		supprimer = new JButton("Supprimer");
		supprimer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (tabFrequent.getSelectedRow() != -1) {
		            System.out.println(tabFrequent.getSelectedRow());
		            model.removeRow(tabFrequent.getSelectedRow());
		            liste.remove(tabFrequent.getSelectedRow());
		        }
				
			}
		});
		top.add(supprimer);
		
		// Partie résultat
		JPanel mid = new JPanel();
		mid.setLayout(new GridLayout(1, 1));
		tabFrequent = new JTable();
		model = (DefaultTableModel) tabFrequent.getModel();
		JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane.setViewportView(tabFrequent);
		tabFrequent.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		mid.add(pane);

		// Partie ajouter mot
		JPanel bot = new JPanel();
		bot.setLayout(new GridLayout(1, 2));
		newMot = new JTextField();
		bot.add(newMot);
		ok = new JButton("Ok");
		bot.add(ok);
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!newMot.getText().toString().equals("")){
				for (int i = model.getRowCount()-1; i >= 0; i--) {
					model.removeRow(i);
				}

				liste.add(newMot.getText());
				
				index = 0;
				datas = new Object[liste.size()][2];
				
				for (String mot : liste) {

					datas[index][0] = liste.get(index);
					datas[index][1] = new JButton("Supprimer");

					model.addRow(datas[index]);
					++index;
				}
				}
			}
		});

		res.add(top, BorderLayout.NORTH);
		res.add(mid, BorderLayout.CENTER);
		res.add(bot, BorderLayout.SOUTH);

		return res;
	}

}
